<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>grammar2.en - racc: Ruby Standard Library Documentation</title>


<script src="../../../js/navigation.js" defer></script>
<script src="../../../js/search.js" defer></script>
<script src="../../../js/search_index.js" defer></script>
<script src="../../../js/searcher.js" defer></script>
<script src="../../../js/darkfish.js" defer></script>

<script src="../../../js/jquery-3.2.0.min.js"></script>

<script src="../../../js/vue.min.js"></script>
<script src="../../../js/js.cookie.min.js"></script>

<link href="../../../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../../../css/rdoc.css" rel="stylesheet">
<link href="../../../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../../../";
  var index_rel_prefix = "../../../";
  var darkModeCsseHref = "../../../css/rdoc-dm.css"
  var defaultModeCssHref = "../../../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="file">
  <!-- this is page.html -->
  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->


 <div class='wrapper hdiv'>

 


<nav id='vapp' role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../../../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../../../table_of_contents.html#pages">Pages</a>
    <a href="../../../table_of_contents.html#classes">Classes</a>
    <a href="../../../table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#label-Racc+Grammar+File+Reference">Racc Grammar File Reference</a>
    <li><a href="#label-Global+Structure">Global Structure</a>
    <li><a href="#label-Class+Block+and+User+Code+Block">Class Block and User Code Block</a>
    <li><a href="#label-Comments">Comments</a>
    <li><a href="#label-Class+Block">Class Block</a>
    <li><a href="#label-Grammar+Block">Grammar Block</a>
    <li><a href="#label-Operator+Precedence">Operator Precedence</a>
    <li><a href="#label-expect">expect</a>
    <li><a href="#label-Declaring+Tokens">Declaring Tokens</a>
    <li><a href="#label-Options">Options</a>
    <li><a href="#label-Converting+Token+Symbol">Converting Token Symbol</a>
    <li><a href="#label-Start+Rule">Start Rule</a>
    <li><a href="#label-User+Code+Block">User Code Block</a>
  </ul>
</div>

  <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>

  <div id="project-metadata">
   <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
    
<div id="fileindex-section" class="nav-section">
  <h3>Pages</h3>

  <ul class="link-list">
    <li><a href="../../../racc-1_7_1/README_ja_rdoc.html">README.ja</a>
    <li><a href="../../../racc-1_7_1/README_rdoc.html">README</a>
    <li><a href="../../../racc-1_7_1/doc/en/NEWS_en_rdoc.html">NEWS.en</a>
    <li><a href="../../../racc-1_7_1/doc/en/grammar_en_rdoc.html">grammar.en</a>
    <li><a href="../../../racc-1_7_1/doc/en/grammar2_en_rdoc.html">grammar2.en</a>
    <li><a href="../../../racc-1_7_1/doc/ja/NEWS_ja_rdoc.html">NEWS.ja</a>
    <li><a href="../../../racc-1_7_1/doc/ja/debug_ja_rdoc.html">debug.ja</a>
    <li><a href="../../../racc-1_7_1/doc/ja/grammar_ja_rdoc.html">grammar.ja</a>
    <li><a href="../../../racc-1_7_1/doc/ja/parser_ja_rdoc.html">parser.ja</a>
  </ul>
</div>

   </div>
  </div>
</nav>


<!--  carbon ads here -->

<div id='extraz'>
  <div class='adzbox-index'  >
   
  </div>
 </div>         


<main role="main" aria-label="Page racc-1.7.1/doc/en/grammar2.en.rdoc">

<h1 id="label-Racc+Grammar+File+Reference"><a href="../../../Racc.html"><code>Racc</code></a> Grammar File Reference<span><a href="#label-Racc+Grammar+File+Reference">&para;</a> <a href="#top">&uarr;</a></span></h1>

<h2 id="label-Global+Structure">Global Structure<span><a href="#label-Global+Structure">&para;</a> <a href="#top">&uarr;</a></span></h2>

<h2 id="label-Class+Block+and+User+Code+Block">Class Block and User Code Block<span><a href="#label-Class+Block+and+User+Code+Block">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>There are two blocks on the toplevel. One is the ‘class’ block, the other is the ‘user code’ block. The ‘user code’ block MUST be placed after the ‘class’ block.</p>

<h2 id="label-Comments">Comments<span><a href="#label-Comments">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>You can insert comments about all places. Two styles of comments can be used, Ruby style ‘#.….’ and C style ‘/*.…..*/’.</p>

<h2 id="label-Class+Block">Class Block<span><a href="#label-Class+Block">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The class block is formed like this:</p>

<pre>class CLASS_NAME
  [precedence table]
  [token declarations]
  [expected number of S/R conflicts]
  [options]
  [semantic value conversion]
  [start rule]
rule
  GRAMMARS</pre>

<p>CLASS_NAME is a name of the parser class. This is the name of the generating parser class.</p>

<p>If CLASS_NAME includes ‘::’, <a href="../../../Racc.html"><code>Racc</code></a> outputs the module clause. For example, writing “class M::C” causes the code below to be created:</p>

<pre>module M
  class C
    :
    :
  end
end</pre>

<h2 id="label-Grammar+Block">Grammar Block<span><a href="#label-Grammar+Block">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The grammar block describes grammar which is able to be understood by the parser. Syntax is:</p>

<pre>(token): (token) (token) (token).... (action)

(token): (token) (token) (token).... (action)
       | (token) (token) (token).... (action)
       | (token) (token) (token).... (action)</pre>

<p>(action) is an action which is executed when its (token)s are found. (action) is a ruby code block, which is surrounded by braces:</p>

<pre>{ print val[0]
  puts val[1] }</pre>

<p>Note that you cannot use ‘%’ string, here document, ‘%r’ regexp in action.</p>

<p>Actions can be omitted. When it is omitted, ” (empty string) is used.</p>

<p>A return value of action is a value of the left side value ($$). It is the value of the result, or the returned value by ‘return` statement.</p>

<p>Here is an example of the whole grammar block.</p>

<pre>rule
  goal: definition rules source { result = val }

  definition: /* none */   { result = [] }
    | definition startdesig  { result[0] = val[1] }
    | definition
             precrule   # this line continues from upper line
      {
        result[1] = val[1]
      }

  startdesig: START TOKEN</pre>

<p>You can use the following special local variables in action:</p>
<ul><li>
<p>result ($$)</p>
</li></ul>

<p>The value of the left-hand side (lhs). A default value is <a href="0">val</a>.</p>
<ul><li>
<p>val ($1,$2,$3…)</p>
</li></ul>

<p>An array of value of the right-hand side (rhs).</p>
<ul><li>
<p>_values (…$-2,$-1,$0)</p>
</li></ul>

<p>A stack of values. DO NOT MODIFY this stack unless you know what you are doing.</p>

<h2 id="label-Operator+Precedence">Operator Precedence<span><a href="#label-Operator+Precedence">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>This function is equal to ‘%prec’ in yacc. To designate this block:</p>

<pre class="ruby"><span class="ruby-identifier">prechigh</span>
  <span class="ruby-identifier">nonassoc</span> <span class="ruby-string">&#39;++&#39;</span>
  <span class="ruby-identifier">left</span>     <span class="ruby-string">&#39;*&#39;</span> <span class="ruby-string">&#39;/&#39;</span>
  <span class="ruby-identifier">left</span>     <span class="ruby-string">&#39;+&#39;</span> <span class="ruby-string">&#39;-&#39;</span>
  <span class="ruby-identifier">right</span>    <span class="ruby-string">&#39;=&#39;</span>
<span class="ruby-identifier">preclow</span>
</pre>

<p>‘right` is yacc’s %right, ‘left` is yacc’s %left.</p>

<p>‘=` + (symbol) means yacc’s %prec:</p>

<pre>prechigh
  nonassoc UMINUS
  left &#39;*&#39; &#39;/&#39;
  left &#39;+&#39; &#39;-&#39;
preclow

rule
  exp: exp &#39;*&#39; exp
     | exp &#39;-&#39; exp
     | &#39;-&#39; exp       =UMINUS   # equals to &quot;%prec UMINUS&quot;
         :
         :</pre>

<h2 id="label-expect">expect<span><a href="#label-expect">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p><a href="../../../Racc.html"><code>Racc</code></a> has bison’s “expect” directive.</p>

<pre># Example

class MyParser
rule
  expect 3
    :
    :</pre>

<p>This directive declares “expected” number of shift/reduce conflicts. If “expected” number is equal to real number of conflicts, <a href="../../../Racc.html"><code>Racc</code></a> does not print conflict warning message.</p>

<h2 id="label-Declaring+Tokens">Declaring Tokens<span><a href="#label-Declaring+Tokens">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>By declaring tokens, you can avoid many meaningless bugs. If declared token does not exist or existing token does not decleared, <a href="../../../Racc.html"><code>Racc</code></a> output warnings. Declaration syntax is:</p>

<pre class="ruby"><span class="ruby-identifier">token</span> <span class="ruby-constant">TOKEN_NAME</span> <span class="ruby-constant">AND_IS_THIS</span>
      <span class="ruby-constant">ALSO_THIS_IS</span> <span class="ruby-constant">AGAIN_AND_AGAIN</span> <span class="ruby-constant">THIS_IS_LAST</span>
</pre>

<h2 id="label-Options">Options<span><a href="#label-Options">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>You can write options for <a href="../../../Racc.html"><code>Racc</code></a> command in your <a href="../../../Racc.html"><code>Racc</code></a> file.</p>

<pre class="ruby"><span class="ruby-identifier">options</span> <span class="ruby-constant">OPTION</span> <span class="ruby-constant">OPTION</span> <span class="ruby-operator">...</span>
</pre>

<p>Options are:</p>
<ul><li>
<p>omit_action_call</p>
</li></ul>

<p>omits empty action call or not.</p>
<ul><li>
<p>result_var</p>
</li></ul>

<p>uses local variable “result” or not.</p>

<p>You can use ‘no_’ prefix to invert their meanings.</p>

<h2 id="label-Converting+Token+Symbol">Converting Token Symbol<span><a href="#label-Converting+Token+Symbol">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Token symbols are, as default,</p>

<pre>* naked token string in Racc file (TOK, XFILE, this_is_token, ...)
  --&gt; symbol (:TOK, :XFILE, :this_is_token, ...)
* quoted string (&#39;:&#39;, &#39;.&#39;, &#39;(&#39;, ...)
  --&gt; same string (&#39;:&#39;, &#39;.&#39;, &#39;(&#39;, ...)</pre>

<p>You can change this default by “convert” block. Here is an example:</p>

<pre>convert
  PLUS &#39;PlusClass&#39;      # We use PlusClass for symbol of `PLUS&#39;
  MIN  &#39;MinusClass&#39;     # We use MinusClass for symbol of `MIN&#39;
end</pre>

<p>We can use almost all ruby value can be used by token symbol, except ‘false’ and ‘nil’.  These cause unexpected parse error.</p>

<p>If you want to use String as token symbol, special care is required. For example:</p>

<pre>convert
  class &#39;&quot;cls&quot;&#39;            # in code, &quot;cls&quot;
  PLUS &#39;&quot;plus\n&quot;&#39;          # in code, &quot;plus\n&quot;
  MIN  &quot;\&quot;minus#{val}\&quot;&quot;   # in code, \&quot;minus#{val}\&quot;
end</pre>

<h2 id="label-Start+Rule">Start Rule<span><a href="#label-Start+Rule">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>‘%start’ in yacc. This changes start rule.</p>

<pre class="ruby"><span class="ruby-identifier">start</span> <span class="ruby-identifier">real_target</span>
</pre>

<h2 id="label-User+Code+Block">User Code Block<span><a href="#label-User+Code+Block">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>“User Code Block” is a Ruby source code which is copied to output. There are three user code blocks, “header” “inner” and “footer”.</p>

<p>Format of user code is like this:</p>

<pre>---- header
  ruby statement
  ruby statement
  ruby statement

---- inner
  ruby statement
     :
     :</pre>

<p>If four ‘-’ exist on the line head, <a href="../../../Racc.html"><code>Racc</code></a> treats it as the beginning of the user code block.  The name of the user code block must be one word.</p>

</main>

</div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>.</p><p><a href="https://jamesbritt.bandcamp.com/">Maximum R+D</a>.  </p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

